package ch.bfh.mobicomp;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.IBinder;
import android.util.Log;

public class PowerListenerService extends Service {
	public static final String INTENT_POWER_STATUS = "ch.bfh.mobicomp.POWER_STATUS";
	public static final String EXTRAS_KEY = "POWER_STATUS";
	public static final String EXTRAS_VALUE_ONAC = "ON_AC";
	public static final String EXTRAS_VALUE_ONBATTERY = "ON_BATTERY";
	public static final String EXTRAS_VALUE_BATTERY_LOW = "BATTERY_LOW";
	public static final String EXTRAS_VALUE_UNKNOWN = "UNKNOWN";
	
	private Intent intentToFire;
	private int batteryLevel;
	
	
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		
		if(intentToFire != null)
		{
			this.fireStatus(intentToFire);
		}
		else
		{
			this.register();
			intentToFire = new Intent(INTENT_POWER_STATUS);
			intentToFire.putExtra(EXTRAS_KEY, EXTRAS_VALUE_UNKNOWN);
			this.fireStatus(intentToFire);
		}

		return super.onStartCommand(intent, flags, startId);
	}
	private void register()
	{
		Log.d("BatteryBroadcastReceiver", "Registrieren beim PowerManager...");
		IntentFilter filter = new IntentFilter();
		filter.addAction("android.intent.action.ACTION_POWER_CONNECTED");
		filter.addAction("android.intent.action.ACTION_POWER_DISCONNECTED");
		filter.addAction("android.intent.action.ACTION_BATTERY_LOW");
		filter.addAction("android.intent.action.ACTION_BATTERY_CHANGED");
		
		
		this.registerReceiver(new BatteryBroadcastReceiver(), filter);
	}
	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}
	
	public void fireStatus(Intent intentToFire)
	{
		//Intent tempIntent = new Intent(state);
		Log.d("BatteryBroadcastReceiver", intentToFire.getExtras().getString("POWER_STATUS"));
		this.sendBroadcast(intentToFire);
	}
	class BatteryBroadcastReceiver extends BroadcastReceiver{

		@Override
		public void onReceive(Context arg0, Intent intent) {
			
			if(intent.getAction().equals("android.intent.action.ACTION_POWER_CONNECTED"))
			{
				intentToFire = new Intent(INTENT_POWER_STATUS);
				intentToFire.putExtra(EXTRAS_KEY, EXTRAS_VALUE_ONAC);
				Log.d("BatteryBroadcastReceiver", "INTENT_ON_AC");
				fireStatus(intentToFire);
			}
			else if(intent.getAction().equals("android.intent.action.ACTION_POWER_DISCONNECTED"))
			{
				intentToFire = new Intent(INTENT_POWER_STATUS);
				intentToFire.putExtra(EXTRAS_KEY, EXTRAS_VALUE_ONBATTERY);
				Log.d("BatteryBroadcastReceiver", "INTENT_ON_BATTERY");
				fireStatus(intentToFire);
			}
			else if(intent.getAction().equals("android.intent.action.ACTION_BATTERY_LOW"))
			{
				intentToFire = new Intent(INTENT_POWER_STATUS);
				intentToFire.putExtra(EXTRAS_KEY, EXTRAS_VALUE_BATTERY_LOW);
				Log.d("BatteryBroadcastReceiver", "INTENT_LOW_BATTERY");
				fireStatus(intentToFire);
			}
			else if(intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED))
			{
				if(batteryLevel != intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1))
				{
					batteryLevel= intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
					intentToFire = new Intent(INTENT_POWER_STATUS);
					intentToFire.putExtra(EXTRAS_KEY, batteryLevel);
					fireStatus(intentToFire);
					Log.d("BatteryBroadcastReceiver", "BatteryLevel:" + batteryLevel);
				}
			}
			
		}
	}

}
